using System;
using UnityEngine;

public enum UpgradeType
{
    Power,
    Size,
    Penetrate,
    Speed
}
[Serializable]
public class SaveData
{
    [SerializeField]
    private int levelIndex = 0;

    [SerializeField]
    private string usingBulletName = null;

    [SerializeField]
    private long coin = 0;

    [SerializeField]
    private int powerLevel = 0;
    [SerializeField]
    private int sizeLevel = 0;
    [SerializeField]
    private int penetrateLevel = 0;
    [SerializeField]
    private int speedLevel = 0;
    public long Coin
    {
        get
        {
            return coin;
        }
        set
        {
            coin = value;
            StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
        }
    }
    public int LevelIndex
    {
        get
        {
            return levelIndex;
        }
        set
        {
            levelIndex = value;
            StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
        }
    }
    public string UsingBulletName
    {
        get
        {
            return usingBulletName;
        }
    }
    public int PowerLevel
    {
        get
        {
            return powerLevel;
        }
        set
        {
            if (value > 9)
            {
                value = 9;
            }
            powerLevel = value;
            StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
        }
    }
    public int SizeLevel
    {
        get
        {
            return sizeLevel;
        }
        set
        {
            if (value > 9)
            {
                value = 9;
            }
            sizeLevel = value;
            StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
        }
    }
    public int PenetrateLevel
    {
        get
        {
            return penetrateLevel;
        }
        set
        {
            if (value > 9)
            {
                value = 9;
            }
            penetrateLevel = value;
            StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
        }
    }
    public int SpeedLevel
    {
        get
        {
            return speedLevel;
        }
        set
        {
            if (value > 9)
            {
                value = 9;
            }
            speedLevel = value;
            StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
        }
    }
    public void SetUsingBulletName(string bulletName)
    {
        usingBulletName = bulletName;
        StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
    }

    public void IncrementLevelIndex()
    {
        levelIndex += 1;
        StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
    }
    public void ChangeCoin(int count)
    {
        coin += count;
        StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
        ((MainMenuPage)UIManager.Instance.panelList[PanelId.MainMenu]).UpdateInfo();
    }
    public void Upgrade(UpgradeType upgradeType)
    {
        switch (upgradeType)
        {
            case UpgradeType.Power:
                if (powerLevel < 10 && coin >= (PowerLevel + 1) * 100)
                {
                    ChangeCoin(-(PowerLevel + 1) * 100);
                    IncrementPowerLevel();
                    AudioManager.Instance.PlayOnShot((int)ClipType.renwushenji);
                }
                else if (powerLevel >= 10)
                {
                    Tip.Show("等级已满");
                }
                else
                {
                    Tip.Show("金币不足");
                }
                break;
            case UpgradeType.Size:
                if (sizeLevel < 10 && coin >= (sizeLevel + 1) * 100)
                {
                    ChangeCoin(-(sizeLevel + 1) * 100);
                    IncrementSizeLevel();
                    AudioManager.Instance.PlayOnShot((int)ClipType.renwushenji);
                }
                else if (sizeLevel >= 10)
                {
                    Tip.Show("等级已满");
                }
                else
                {
                    Tip.Show("金币不足");
                }
                break;
            case UpgradeType.Penetrate:
                if (penetrateLevel < 10 && coin >= (penetrateLevel + 1) * 100)
                {
                    ChangeCoin(-(penetrateLevel + 1) * 100);
                    IncrementPenetrateLevel();
                    AudioManager.Instance.PlayOnShot((int)ClipType.renwushenji);
                }
                else if (penetrateLevel >= 10)
                {
                    Tip.Show("等级已满");
                }
                else
                {
                    Tip.Show("金币不足");
                }
                break;
            case UpgradeType.Speed:
                if (speedLevel < 10 && coin >= (speedLevel + 1) * 100)
                {
                    ChangeCoin(-(speedLevel + 1) * 100);
                    IncrementSpeedLevel();
                    AudioManager.Instance.PlayOnShot((int)ClipType.renwushenji);
                }
                else if (speedLevel >= 10)
                {
                    Tip.Show("等级已满");
                }
                else
                {
                    Tip.Show("金币不足");
                }
                break;
        }
    }
    public void IncrementPowerLevel()
    {
        powerLevel += 1;
        StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
    }
    public void IncrementSizeLevel()
    {
        sizeLevel += 1;
        StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
    }

    public void IncrementPenetrateLevel()
    {
        penetrateLevel += 1;
        StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
    }

    public void IncrementSpeedLevel()
    {
        speedLevel += 1;
        StorageManager.Instance.Save<SaveData>(Const.SaveDataName, this);
    }

    public SaveData()
    {
        levelIndex = 0;
        usingBulletName = null;
        coin = 0;
        powerLevel = 0;
        sizeLevel = 0;
        penetrateLevel = 0;
        speedLevel = 0;
    }
}
